using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aplicacion.Base.WinForm;
using Comercial.Base;
using BaseDatos.Comun;
using BaseDatos;
using TDV.Controles.Base.WinForm;
using System.Reflection;

namespace SCHD
{
    public partial class WFAtributos : TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        #region Campos
        protected string codigo = "";
        LBAtributo Atributos_MOD_ = new LBAtributo();
        LBEquipoxAtributo eqpxatributoMod = new LBEquipoxAtributo();
        #endregion
        #region Constructores
        public WFAtributos()
        {
            InitializeComponent();
        }
        public WFAtributos(string Codigo)
            :this()
        {
            codigo = Codigo;
        }
        #endregion
        private void btnCancelar_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void WFAtributos_Load(object sender, EventArgs e)
        {
            txtatributo.CharacterCasing = CharacterCasing.Upper;
            CargarAtribEquipo();
            this.KeyPreview = true;
            CargarAtributo();
        }
        protected void CargarAtribEquipo()
        {
            try
            {
                // Modificado para que no guarde en la memoria
                LBEquipoxAtributo equipoxatributo = new LBEquipoxAtributo();
                equipoxatributo.Filtros.CodigoCategoria = codigo.ToString().Substring(0, 3);
                equipoxatributo.Filtros.CodigoTipo = codigo.ToString().Substring(3, 3);
                equipoxatributo.Filtros.CodigoClase = codigo.ToString().Substring(6, 3);
                equipoxatributo.Buscar();
                dgEquipoxatributo.DataSource = equipoxatributo;
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        protected void CargarAtributo()
        {
            LBAtributo atrib = new LBAtributo();
            atrib.Filtros.CodigoCategoria = codigo.ToString().Substring(0, 3);
            atrib.Filtros.CodigoTipo=codigo.ToString().Substring(3, 3);
            atrib.Filtros.CodigoClase = codigo.ToString().Substring(6, 3);
            atrib.Buscar();
            dgatributo.DataSource = atrib;
        }

        private void btnasignar_Click(object sender, EventArgs e)
        {
            Grabar();
            foreach (DataGridViewRow row in dgatributo.Rows)
            {
                DataGridViewCheckBoxCell ck1 = row.Cells[0] as  DataGridViewCheckBoxCell;

                //CheckBox ck = (CheckBox)row.Cells[0].Value;
                if (Convert.ToBoolean(ck1.Value))
                {
                    EBEquipoxAtributo eqpxatrib = new EBEquipoxAtributo();
                    eqpxatrib.CodigoCategoria = codigo.ToString().Substring(0,3);
                    eqpxatrib.CodigoTipo = codigo.ToString().Substring(3,3);
                    eqpxatrib.CodigoClase = codigo.ToString().Substring(6,3);
                    eqpxatrib.CodigoAtributo = row.Cells[1].Value.ToString();
                    eqpxatrib.Grabar();
                }
            }
            CargarAtribEquipo();
            CargarAtributo();
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            EBAtributo atributo = new EBAtributo();
            if (chkUnico.Checked)
            {
                atributo.FlagUnico = "1";
            }
            else
            {
                atributo.FlagUnico = "0";
            }

            atributo.Descripcion = txtatributo.Text.Trim().ToUpper();
            atributo.Grabar();
            CargarAtribEquipo();
            CargarAtributo();
            txtatributo.Text = "";
            txtatributo.Focus();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            Grabar();
            DialogResult = DialogResult.OK;
        }
        protected void Grabar()
        {
            foreach (EBAtributo __Atributo in Atributos_MOD_)
            {
                __Atributo.Grabar();
            }

            foreach (EBEquipoxAtributo __EqpxAtributo in eqpxatributoMod)
            {
                __EqpxAtributo.Grabar();
            }
            Atributos_MOD_.Clear();
            eqpxatributoMod.Clear();
        }

        private void dgatributo_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
            if (dgatributo.Columns[e.ColumnIndex].Name != null)
            {
                // Se toma la fila seleccionada 
                if (e.RowIndex > -1)
                {
                    DataGridViewRow row = dgatributo.Rows[e.RowIndex];
                    
                    string Codigo = row.Cells[1].Value.ToString();
                    string Descripcion = row.Cells[2].Value.ToString();
                    string unico = row.Cells[3].Value.ToString();
                    //string Valor = row.Cells[e.ColumnIndex].Value.ToString();
                    //string Restriccion = GrdAtributos.Columns[e.ColumnIndex].Name.ToString();
                    //DataGridViewCheckBoxCell cellSelecion = row.Cells[e.RowIndex] as DataGridViewCheckBoxCell;
                    EBAtributo Atributo = new EBAtributo();
                    Atributo.CodigoAtributo = Codigo;
                    Atributo.Descripcion=Descripcion;
                    Atributo.FlagUnico = unico;
                    Atributos_MOD_.Nuevo(Atributo);
                }

            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgatributo.Rows)
            {
                DataGridViewCheckBoxCell ck2 = row.Cells[0] as DataGridViewCheckBoxCell;

                //CheckBox ck = (CheckBox)row.Cells[0].Value;
                if (Convert.ToBoolean(ck2.Value))
                {
                    try
                    {
                        EBAtributo atributo = new EBAtributo();
                        atributo.CodigoAtributo = row.Cells["CodigoAtributo"].Value.ToString();
                        atributo.Eliminar();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString(),"Aviso",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                    }
                }
            }
            CargarAtributo();
        }

        private void dgEquipoxatributo_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewRow row = dgEquipoxatributo.Rows[e.RowIndex];
                EBEquipoxAtributo eqpxatributo = new EBEquipoxAtributo();
                eqpxatributo.CodigoCategoria = codigo.ToString().Substring(0, 3);
                eqpxatributo.CodigoTipo = codigo.ToString().Substring(3, 3);
                eqpxatributo.CodigoClase = codigo.ToString().Substring(6, 3);
                eqpxatributo.CodigoAtributo = row.Cells["Codig"].Value.ToString();
                eqpxatributo.CodigoSecuencia = Int32.Parse(row.Cells["Ord"].Value.ToString());
                eqpxatributoMod.Nuevo(eqpxatributo);
            }
        }

        private void WFAtributos_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Escape)
                DialogResult = DialogResult.Cancel;
        }

        private void btnQuitar_Click(object sender, EventArgs e)
        {
            Grabar();
            foreach (DataGridViewRow row1 in dgEquipoxatributo.Rows)
            {
                DataGridViewCheckBoxCell ck3 = row1.Cells["dgck"] as DataGridViewCheckBoxCell;

                //CheckBox ck = (CheckBox)row.Cells[0].Value;
                if (Convert.ToBoolean(ck3.Value))
                {
                    try
                    {
                        EBEquipoxAtributo eqpxatributo = new EBEquipoxAtributo();
                        eqpxatributo.CodigoCategoria = codigo.ToString().Substring(0, 3);
                        eqpxatributo.CodigoTipo = codigo.ToString().Substring(3, 3);
                        eqpxatributo.CodigoClase = codigo.ToString().Substring(6, 3);
                        eqpxatributo.CodigoAtributo = row1.Cells["Codig"].Value.ToString(); ;
                        eqpxatributo.Eliminar();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
                
            }
            
            CargarAtribEquipo();
            CargarAtributo();
        }

        private void txtatributo_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgEquipoxatributo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
             if (dgEquipoxatributo.CurrentCell.ColumnIndex == 1) 
        {

         TextBox txt = e.Control as TextBox;

        if (txt != null) {
            //txt.KeyPress -= new KeyPressEventHandler(dataGridDatos_KeyPress);
            //txt.KeyPress += new KeyPressEventHandler(dataGridDatos_KeyPress);

            txt.KeyPress -= new KeyPressEventHandler(dgEquipoxatributo_KeyPress);
            txt.KeyPress += new KeyPressEventHandler(dgEquipoxatributo_KeyPress);
        }

        }
        }

        private void dgEquipoxatributo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar) || e.KeyChar == (Char)Keys.Back)
                e.Handled = false;
            else
                e.Handled = true;
        }

        private void baseButton1_Click(object sender, EventArgs e)
        {
            dgatributo.DataSource = null;
            
            LBAtributo atributo = new LBAtributo();
            
            if (BtnAtributos.Text == "Todos")
            {
                atributo.Filtros.Opcion = "QRY03";
                atributo.Buscar();
                BtnAtributos.Text = "Normal";
            }
            else
            {
                atributo.Filtros.CodigoCategoria = codigo.ToString().Substring(0, 3);
                atributo.Filtros.CodigoTipo = codigo.ToString().Substring(3, 3);
                atributo.Filtros.CodigoClase = codigo.ToString().Substring(6, 3);
                atributo.Buscar();
                BtnAtributos.Text = "Todos";
            }
            dgatributo.DataSource = atributo;
        }

        private void txtatributo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnAgregar_Click(sender, e);
            }
        }

        private void TsbSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TsbModificar_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ActiveControl is BaseDataGridView)
                {
                   string codigo =  ((BaseDataGridView)this.ActiveControl).CurrentRow.Cells[1].Value.ToString();
                   WFAtributos_edit atrib = new WFAtributos_edit(codigo);
                   if (atrib.ShowDialog() == DialogResult.OK)
                   {
                       CargarAtribEquipo();
                       CargarAtributo();
                   }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


            

        }


        
    }
}

